package com.ngovanhau.justsort.model;

import com.ngovanhau.justsort.MySprite;
import com.ngovanhau.justsort.item.config.MyConfig;
import com.ngovanhau.justsort.util.UtilActivity;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
 
import android.content.Context;
 
public class Star extends MySprite {
    int max = 10;
    private BitmapTextureAtlas star_BTA;
 
    private TextureRegion star_TR;
 
    int timedelay = 20;
 
    public void addStar(final int x, final int y, int bonus) {
        show1(x, y, bonus);
    }
 
    public TextureRegion getTextureRegion() {
        return star_TR;
    }
 
    public void onLoadResources(Engine mEngine, Context mContext) {
        this.engine = mEngine;
        this.context = mContext;
 
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("images/");
        this.star_BTA = new BitmapTextureAtlas(64, 64,
                TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        this.star_TR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
                this.star_BTA, mContext, "particleSparkle.png", 0, 0);
        mEngine.getTextureManager().loadTextures(this.star_BTA);
    }
 
    public void onLoadScene(Scene mScene) {
        this.scene = mScene;
    }
 
    public void removeStar(final Sprite[] sp) {
        engine.runOnUpdateThread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < sp.length; i++){
                    scene.detachChild(sp[i]);
                }
            }
        });
    }
 
    public void show1(final int x, final int y, final int bonus) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Sprite[] star_SP = new Sprite[bonus*bonus];
                int min = (int) -MyConfig.WIDTH_BG /2;
                int max = (int) MyConfig.WIDTH_BG / 2;
 
                int w = (int) (star_TR.getWidth() * MyConfig.getRaceWidth());
                int h = star_TR.getHeight() * w / star_TR.getWidth();
 
                for (int i = 0; i < star_SP.length; i++) {
 
                    int x_tmp = UtilActivity.getRandomIndex(min, max);
                    x_tmp = x + x_tmp;
 
                    int y_tmp = UtilActivity.getRandomIndex(min, max);
                    y_tmp = y + y_tmp;
 
                    star_SP[i] = new Sprite(x_tmp, y_tmp, w, h, star_TR);
                    scene.attachChild(star_SP[i]);
                    star_SP[i].setZIndex(300);
                }
                int count = 0;
                float pScale = 1f;
                int maxSize = (int) MyConfig.WIDTH_NUMBER/6;
                while (count < maxSize) {
                    try {
                        Thread.sleep(timedelay);
                        count++;
                        for (int i = 0; i < star_SP.length; i++) {
                            int x_tmp = UtilActivity.getRandomIndex(-5, 5);
                            int y_tmp = UtilActivity.getRandomIndex(1, 4);
 
                            star_SP[i].setPosition(star_SP[i].getX() + x_tmp,
                                    star_SP[i].getY() + y_tmp);
                            star_SP[i].setScale(pScale);
                        }
                        pScale = pScale - 0.09f;
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                count = 0;
                while (count < maxSize) {
                    try {
                        Thread.sleep(timedelay);
                        count++;
                        for (int i = 0; i < star_SP.length; i++) {
                            int x_tmp = UtilActivity.getRandomIndex(-5, 5);
                            int y_tmp = UtilActivity.getRandomIndex(1, 4);
 
                            star_SP[i].setPosition(star_SP[i].getX() + x_tmp,
                                    star_SP[i].getY() + y_tmp);
                            star_SP[i].setScale(pScale);
                        }
                        pScale = pScale + 0.09f;
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                removeStar(star_SP);
            }
        }).start();
    }
}